package com.haohan.jingyuan.user.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.user.dto.req.CreateRoleRequest;
import com.haohan.jingyuan.user.dto.req.DeleteRoleRequest;
import com.haohan.jingyuan.user.dto.req.QueryResourceListRequest;
import com.haohan.jingyuan.user.dto.req.QueryRoleListRequest;
import com.haohan.jingyuan.user.dto.req.UpdateRoleRequest;
import com.haohan.jingyuan.user.dto.resp.ResourceListResp;
import com.haohan.jingyuan.user.dto.resp.RoleInfoResp;
import com.haohan.jingyuan.user.dto.resp.RoleListResp;
import com.haohan.jingyuan.user.service.ResourceService;
import com.haohan.jingyuan.user.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 角色控制器
 *
 * @ClassName RoleController
 * @Description 角色相关接口控制器
 * @Author hexijun
 * @Time 2025/01/19 10:30
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class RoleController {

    /**
     * 角色服务
     */
    private final RoleService roleService;

    /**
     * 资源服务
     */
    private final ResourceService resourceService;

    /**
     * 创建角色
     *
     * @param request 创建角色请求
     * @return 创建结果
     */
    @NeedLogin
    @PostMapping("/admin/role/create")
    public GwResponse<RoleInfoResp> createRole(@Valid @RequestBody CreateRoleRequest request) {
        log.info("RoleController.createRole BEGIN ==> {}", JSONUtil.toJsonStr(request));

        RoleInfoResp roleInfo = roleService.createRole(request);
        GwResponse<RoleInfoResp> response = GwResponse.success("创建角色成功", roleInfo);

        log.info("RoleController.createRole END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 更新角色
     *
     * @param request 更新角色请求
     * @return 更新结果
     */
    @NeedLogin
    @PostMapping("/admin/role/update")
    public GwResponse<RoleInfoResp> updateRole(@Valid @RequestBody UpdateRoleRequest request) {
        log.info("RoleController.updateRole BEGIN ==> {}", JSONUtil.toJsonStr(request));

        RoleInfoResp roleInfo = roleService.updateRole(request);
        GwResponse<RoleInfoResp> response = GwResponse.success("更新角色成功", roleInfo);

        log.info("RoleController.updateRole END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除角色
     *
     * @param request 删除角色请求
     * @return 删除结果
     */
    @NeedLogin
    @PostMapping("/admin/role/delete")
    public GwResponse<String> deleteRole(@Valid @RequestBody DeleteRoleRequest request) {
        log.info("RoleController.deleteRole BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = roleService.deleteRole(request);
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除角色成功", null);
        } else {
            response = GwResponse.error("删除角色失败");
        }

        log.info("RoleController.deleteRole END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询角色列表
     *
     * @param request 查询角色列表请求
     * @return 角色列表
     */
    @NeedLogin
    @PostMapping("/admin/role/list")
    public GwResponse<RoleListResp> queryRoleList(@Valid @RequestBody GwRequest<QueryRoleListRequest> request) {
        log.info("RoleController.queryRoleList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        RoleListResp roleList = roleService.queryRoleList(request.getRequestData());
        GwResponse<RoleListResp> response = GwResponse.success("查询角色列表成功", roleList);

        log.info("RoleController.queryRoleList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询资源列表
     *
     * @param request 查询资源列表请求
     * @return 资源列表
     */
    @NeedLogin
    @PostMapping("/admin/resource/list")
    public GwResponse<ResourceListResp> queryResourceList(@Valid @RequestBody GwRequest<QueryResourceListRequest> request) {
        log.info("RoleController.queryResourceList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        ResourceListResp resourceList = resourceService.queryResourceList(request.getRequestData());
        GwResponse<ResourceListResp> response = GwResponse.success("查询资源列表成功", resourceList);

        log.info("RoleController.queryResourceList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}
